package com.persondemo.videoappliction.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.persondemo.videoappliction.R;
import com.persondemo.videoappliction.bean.VideoBean;
import com.persondemo.videoappliction.event.MainEvent;
import com.persondemo.videoappliction.utils.T;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * GridView 适配器
 */
public class MyGridViewAdapter extends BaseAdapter {
    private Context mContext;
    private List<Boolean> isClicks;//控件是否被点击,默认为false，如果被点击，改变值，控件根据值改变自身颜色
    /**
     * 每个分组下的每个子项的 GridView 数据集合
     */
    private List<VideoBean.DataBean.LaiyuanBean.JishuBean> itemGridList;

    public MyGridViewAdapter(Context mContext, List<VideoBean.DataBean.LaiyuanBean.JishuBean> itemGridList) {
        this.mContext = mContext;
        this.itemGridList = itemGridList;
        isClicks = new ArrayList<>();
        for (int i = 0; i < itemGridList.size(); i++) {
            isClicks.add(false);
        }
    }

    @Override
    public int getCount() {
        return itemGridList.size();
    }

    @Override
    public Object getItem(int position) {
        return itemGridList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = View.inflate(mContext, R.layout.gridview_item, null);
        }
        TextView tvGridView = (TextView) convertView.findViewById(R.id.tv_gridview);
        tvGridView.setText(itemGridList.get(position).getLaiji());
        if (isClicks.get(position)) {
            tvGridView.setTextColor(Color.parseColor("#F68E54"));
        } else {
            tvGridView.setTextColor(Color.parseColor("#ffffff"));
        }
        tvGridView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < isClicks.size(); i++) {
                    isClicks.set(i, false);
                }
                EventBus.getDefault().post(new MainEvent(itemGridList.get(position).getUrls(),itemGridList.get(position).getLaiji()));
                isClicks.set(position, true);
                notifyDataSetChanged();
            }
        });
        return convertView;
    }
}
